/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.nodes;

import java.awt.Image;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Utilities;

/**
 *
 * @author lforet
 */
public abstract class AbstractFolderNode extends AbstractNode {

    private static Image folderIconCache;
    private static Image openedFolderIconCache;

    public AbstractFolderNode(Children children) {
        super(children);
    }
    
    /**
     * Returns Icon of folder on active platform
     * @param opened should the icon represent opened folder
     * @return the folder icon
     */
    private static synchronized Image getFolderIcon(boolean opened) {
        if (opened) {
            if (openedFolderIconCache == null) {
                openedFolderIconCache = NodeUtils.getTreeFolderIcon(opened);
            }
            return openedFolderIconCache;
        } else {
            if (folderIconCache == null) {
                folderIconCache = NodeUtils.getTreeFolderIcon(opened);
            }
            return folderIconCache;
        }
    }

    private Image computeIcon(boolean opened, int type) {
        Image image = getFolderIcon(opened);
        image = Utilities.mergeImages(image, getBadge(), 7, 7);
        return image;
    }

    @Override
    public Image getIcon(int type) {
        return computeIcon(false, type);
    }

    @Override
    public Image getOpenedIcon(int type) {
        return computeIcon(true, type);
    }

    protected abstract Image getBadge();
    
}
